#include "storageitem.h"
#include "ui_storageitem.h"
#include "QPushButton"
#include "storageedit.h"
StorageItem::StorageItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StorageItem)

{
    ui->setupUi(this);

}

void StorageItem::update(Storage storage){
    this->storage = storage;
    ui->label_name->setText(this->storage.name);
    ui->lable_image->showURLImage(this->storage.pic);
    ui->label_amount->setText("数量: "+QString::number(this->storage.amount));
    connect(ui->btn_edit,&QPushButton::clicked,this,[=](){
        StorageEdit* sd = new StorageEdit(this);
        sd->update(this->storage);
        connect(sd,&StorageEdit::confirm,this,[=](int amount){
            this->storage.amount = amount;
            ui->label_amount->setText("数量: "+QString::number(amount));
            // 执行更新，更新当前 TODO sql更新
            sql->storageUpdate(this->storage.user_id,this->storage.food_id,this->storage.amount);
        });
        sd->show();
    });
    connect(ui->btn_delete,&QPushButton::clicked,this,[=](){
        emit deleteSignal();
        sql->storageDelete(this->storage.user_id,this->storage.food_id);
        // sql 删除
    });

}

StorageItem::~StorageItem()
{
    delete ui;
}
